// import Pinyin from 'pinyin'
import store from '../store/index'
 
function showToast(title) {
	wx.showToast({
		icon: 'none',
		title
	})
}

function showModal(title) {
	return new Promise((resolve, reject)=> {
		wx.showModal({
			title: '提示',
			content: title,
			success: function(res) {
				if (res.confirm) {
					resolve(res)
				} else if (res.cancel) {
					reject('cancel')
				}
			}
		})
	})
}

function wxShare(shareOpt,ownOpt) {
	console.log(shareOpt, ownOpt);
	
	addIntergral().then(res=> {
		console.log(res);
	})
	return {
		title: ownOpt&&ownOpt.title || '快来看看运气如何吧',
		imageUrl: ownOpt&&ownOpt.img ||(shareOpt.from=='button'&&ownOpt.url)||'',
    path: shareOpt? ownOpt&&ownOpt.url: '',
	}
}


// pxTorpx
function pxTorpx(px) {
	return new Promise((resolve, reject) => {
		wx.getSystemInfo({
			success(info) {
				let rate = 750 / info.windowWidth
				resolve(px? px * rate: rate)
			},
			fail(err) {
				resolve(px? px * 2: 2) // 给个异常的默认返回值
			}
		})
	})
}

// 判断是不是Iphone X
function isIphoneX() {
	let isX = false
	return new Promise((resolve, reject)=> {
		wx.getSystemInfo({
			success: function(res) {
				let names = ['iPhone X', 'iPhoneX', 'iphone X', 'iphoneX']
				names.forEach(item=> {
					if (res.model.indexOf(name) > -1) isX = true
				})
				resolve(isX)
			},
			fail(err) {
				console.log('isIphoneX', err)
				resolve(isX)
			}
		})
	})
}

function addFormId(opt) {
	let formid = opt.target.formId
	if (opt&& formid && !(/[a,z]/.test(formid))) {
		let formidStore = store.state.formId
		console.log('add formid',formid);
		
		formidStore.push(formid)
		store.commit('setFormid', formidStore)
	}
}

function formateTime(timeString) {
	if (!timeString) return ''
	
	let fullNum = function(num) { return num>9?num: '0'+num}
	let time = new Date(timeString)

	let str = `${time.getFullYear()}年${fullNum(time.getMonth()+1)}月${fullNum(time.getDate())}日
	${fullNum(time.getHours())}:${fullNum(time.getMinutes())}:${fullNum(time.getSeconds())}`
	return str
}

function formateDate(timeString) {
	if (!timeString) return ''
	
	let fullNum = function(num) { return num>9?num: '0'+num}
	let time = new Date(timeString)

	let str = `${time.getFullYear()}-${fullNum(time.getMonth()+1)}-${fullNum(time.getDate())}`
	return str
}

// 按数字截取字符串
function spliteByNum(str, spliteNum, isAverage) {
	const res = []
	if (!str || !spliteNum) return res
	str = JSON.parse(JSON.stringify(str))
	if (isAverage && str.length < 2* spliteNum) {
		res.push(str.substr(0,str.length/2))
		res.push(str.substr(str.length/2,str.length/2+1))
	} else {
		for (let i = 0; i < str.length/spliteNum; i++) {
			res.push(str.substr(i*spliteNum,spliteNum))
		}
	}
	return res
}


export {
	showToast, showModal, wxShare, pxTorpx, isIphoneX,formateDate, formateTime, addFormId, spliteByNum,
	
}